/* eslint-disable camelcase */
import { ApiResponse, Article, Channel } from '@/types/data'
import { HomeAction, RootThunkAction } from '@/types/store'
import request from '@/utils/request'
import { hasToken } from '@/utils/storage'
import { getChannels, setChannels } from '@/utils/storageChannel'
// 获取频道数据
export const getUserChannel = (): RootThunkAction => {
  return async (dispatch) => {
    if (hasToken()) {
      const res = await request.get<ApiResponse<{channels:Channel[]}>>('/user/channels')
      const { channels } = res.data.data
      console.log('频道', channels)
      dispatch({
        type: 'home/saveUserChannels',
        payload: res.data.data.channels
      })
    } else {
      const channels = getChannels()

      if (channels.length > 0) {
        // 本地有数据
        dispatch({
          type: 'home/saveUserChannels',
          payload: channels
        })
      } else {
        // 本地没有数据，从网络获取，且存储到本地
        const res = await request.get<ApiResponse<{channels:Channel[]}>>('/user/channels')
        dispatch({
          type: 'home/saveUserChannels',
          payload: res.data.data.channels
        })
        // 存储到本地
        setChannels(res.data.data.channels)
      }
    }
  }
}

// 修改频道高亮
export function changeActive (id:string):HomeAction {
  return {
    type: 'home/changeActive',
    payload: id
  }
}

// 获取所有频道的数据
export function getAllChannel ():RootThunkAction {
  return async dispatch => {
    const res = await request.get<ApiResponse<{channels:Channel[]}>>('/channels')
    dispatch({
      type: 'home/saveAllChannels',
      payload: res.data.data.channels
    })
  }
}
// 添加频道
export const addChannel = (channel:Channel):RootThunkAction => {
  return async (dispatch, getState) => {
    // 获取所有的userChannels
    const { userChannels } = getState().home
    // 登录
    if (hasToken()) {
      await request.patch('/user/channels', { channels: [channel] })
    } else {
      // 没有登录
      // 把channels 数据保存在本地
      setChannels([...userChannels, channel])
    }
    dispatch({
      type: 'home/saveUserChannels',
      payload: [...userChannels, channel]
    })
  }
}
// 删除频道
export function delChannel (id:number):RootThunkAction {
  return async (dispatch, getState) => {
    const { userChannels } = getState().home
    if (hasToken()) {
      // 登录状态
      await request.delete('/user/channels', {
        data: {
          channels: [id]
        }
      })
    } else {
      // 未登录 本地删除
      setChannels(userChannels.filter(item => item.id !== id))
    }
    dispatch(getUserChannel())
  }
}
// 获取文章列表
export const getArticleList = (
  // eslint-disable-next-line camelcase
  channel_id:number,
  timestamp:number
):RootThunkAction => {
  return async dispatch => {
    const res = await request.get<ApiResponse<{pre_timestamp:number, results:Article[]}>>('articles', { params: { channel_id, timestamp } })
    console.log(res, 556)
    // const { pre_timestamp, results } = res.data.data
    dispatch({
      type: 'home/saveChannelArticles',
      payload: {
        channel_id,
        timestamp: res.data.data.pre_timestamp,
        articles: res.data.data.results
      }
    })
  }
}

// 下拉刷新，获取新的文章信息
export const getNewList = (
  channel_id: number
): RootThunkAction => {
  return async (dispatch) => {
    const res = await request.get<
      ApiResponse<{pre_timestamp: number, results: Article[]}>
    >('articles', {
      params: {
        channel_id,
        timestamp: Date.now()
      }
    })
    // const { pre_timestamp, results } = res.data.data
    dispatch({
      type: 'home/getNewArticles',
      payload: {
        channel_id,
        timestamp: res.data.data.pre_timestamp,
        articles: res.data.data.results
      }
    })
  }
}
